using System;
using System.Collections.Generic;
using System.Text;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Common;
using Sedna.Core.Controller.Configurations;

namespace Sedna.Core.Controls.Actions
{
    public class CopyValueAction : AbstractAction
    {
        protected object value = null;
        protected string from = null;
        protected string to = null;
        protected IDataTranslator valueParser;

        # region Properties

        public object Value
        {
            set { this.value = value; }
        }

        public string From
        {
            set { from = value; }
        }

        public string To
        {
            set { to = value; }
        }

        public IDataTranslator ValueParser
        {
            set { valueParser = value; }
        }

        # endregion

        public override IActionResult Execute(IExecutionContext executionContext)
        {
            if (!String.IsNullOrEmpty(to))
            {
                if (!String.IsNullOrEmpty(from))
                {
                    executionContext.SetPropertyValue(to,
                                                      valueParser == null
                                                          ? executionContext.GetPropertyValue(from)
                                                          :
                                                      valueParser.Translate(null,
                                                                            executionContext.GetPropertyValue(from)));
                }
                else
                    executionContext.SetPropertyValue(to, valueParser == null ? value : valueParser.Translate(null, value));                                
            }
            
            return CreateResult();
        }
    }
}
